package com.example.web.servlet;

import com.example.common.Result;
import com.example.model.Trip;
import com.example.service.TripService;
import com.example.service.impl.TripServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.bcel.internal.generic.NEW;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/booking")
public class BookingServlet extends HttpServlet {
    private TripService tripService = new TripServiceImpl();
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Trip> trips = tripService.booking();
        Result result = Result.success(trips);
        String result_str = objectMapper.writeValueAsString(result);
        resp.getWriter().write(result_str);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
